package cn.exlu.coco.core.utils;

import java.util.Date;
import java.util.Random;

public class StringUtil {
    public final static String BLANK = "";
    public final static String CHAR_COMMA = ",";

    public final static String ENCODE_UTF8 = "UTF-8";
    public final static String ENCODE_GBK = "GBK";
    
    /**
     * null 空字符串
     * @param str
     * @return
     */
	public final static boolean isNullOrBlank(String str){
		if(str == null || "".equals(str)){
			return true;
		}
		
		return false;
	}
	
	/**
	 * null 只含有空白字符串
	 * @param str
	 * @return
	 */
	public final static boolean isNullOrEmpty(String str){
		if(str == null){
			return true;
		}
		
		if("".equals(str.trim())){
			return true;
		}
		
		return false;
	}

   public  final static boolean isEqualsVal(String val1, String val2){
       if(isNullOrBlank(val1) && isNullOrBlank(val2)){
           return true;
       }
       
       if(isNullOrBlank(val1) && !isNullOrBlank(val2)){
           return false;
       }
       
       if(!isNullOrBlank(val1) && isNullOrBlank(val2)){
           return false;
       }
       
       return val1.equals(val2);
       
   }

   public final static String covertIntToString(Integer o){
       if(o == null){
           return null;
       }
       return String.valueOf(o);
   }
   
   public  final static String covertDateBusToString(Date o){
       if(o == null){
           return null;
       }

       return DateUtil.formatDateBus(o);
   }
   
   /**
    * 生成随机数
    * @param length
    * @return
    */
   public static String createRandomNum(int length){
       StringBuilder sb = new StringBuilder(); 
       Random random = new Random();     
       for(int i = 0; i < length; i++){
           sb.append(String.valueOf(random.nextInt(10)));
       }
       
       return sb.toString();
       
   }

   /** 
    * 生成随机数字和字母
    * @param length 生成长度
    * @return 
    */ 
   public static String createRandomAphaNum(int length) { 
       StringBuilder sb = new StringBuilder(); 
       Random random = new Random();     
       //参数length，表示生成几位随机数  
       for(int i = 0; i < length; i++) {
           int randNum = random.nextInt(2);
           if(randNum % 2 == 0){
               //输出是大写字母还是小写字母  
               int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
               sb.append((char)(random.nextInt(26) + temp));
           }else{
               sb.append(String.valueOf(random.nextInt(10)));
           }
           
       }  
       return sb.toString(); 
   }
   
   /** 
    * 生成随机数字和字母和符号-_
    * @param length 生成长度
    * @return 
    */ 
   public static String createRandomSalt(int length) { 
       StringBuilder sb = new StringBuilder(); 
       Random random = new Random();     
       //参数length，表示生成几位随机数  
       for(int i = 0; i < length; i++) {
           int randNum = random.nextInt(10);
           if(randNum % 10 == 0){
               sb.append(random.nextInt(2) % 2 == 0 ? "_" : "-");
               continue;
           }
           
           if(randNum % 2 == 0){
               //输出是大写字母还是小写字母  
               int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
               sb.append((char)(random.nextInt(26) + temp));
           }else{
               sb.append(String.valueOf(random.nextInt(10)));
           }
           
       }  
       return sb.toString();  
   }
   
   public static final String lpad(String val, char c, int maxLen){
       int len = maxLen-val.length();
       if(len <= 0){
           return val;
       }
       
       StringBuilder sb = new StringBuilder();
       for(int i=0; i<len; i++){
           sb.append(c);
       }
       sb.append(val);
       
       return sb.toString();
   }

//   public static void main(String[] args) {
//       String str = createRandomNum(6);
//       System.out.println(str);
//       
//   }

}
